package weaver;

import weaver.model.WeaverModel;
import java.util.Scanner;
import java.util.List;

/**
 * Main class for the CLI version of the Weaver game.
 * This version directly uses the model without view or controller.
 * 
 * The program behavior is controlled by three flags:
 * 1. isValidating: Controls whether to display error messages for invalid words
 * 2. isShowingPath: Controls whether to display the path from start to target word
 * 3. isRandomWords: Controls whether to randomly select start and target words
 */
public class WeaverCLI {
    public static void main(String[] args) {
        assert args != null : "Arguments should not be null";
        WeaverModel model = new WeaverModel();
        Scanner scanner = new Scanner(System.in);

        try {
            // Set flags based on user input
            System.out.println("Set game options (y/n):");
            System.out.print("Validate words? ");
            boolean isValidating = scanner.nextLine().trim().toLowerCase().equals("y");
            System.out.print("Show path? ");
            boolean isShowingPath = scanner.nextLine().trim().toLowerCase().equals("y");
            System.out.print("Use random words? ");
            boolean isRandomWords = scanner.nextLine().trim().toLowerCase().equals("y");
            
            model.setFlags(isValidating, isShowingPath, isRandomWords);
            
            // Get start and target words
            String startWord, targetWord;
            if (isRandomWords) {
                startWord = model.getRandomWord();
                targetWord = model.getRandomWord();
                System.out.println("Random start word: " + startWord.toUpperCase());
                System.out.println("Random target word: " + targetWord.toUpperCase());
            } else {
                startWord = "COLD";
                targetWord = "WARM";
                System.out.println("Start word: " + startWord);
                System.out.println("Target word: " + targetWord);
            }
            
            // Set the words and start the game
            model.setWords(startWord, targetWord);
            
            // Game loop
            while (true) {

                System.out.println("--------------------------------------------------");
                // Display current game state
                System.out.println("\nStart word: " + model.getCurrentPath().get(0).toUpperCase());
                System.out.println("Previous word: " + model.getCurrentPath().get(model.getCurrentPath().size() - 1));
                System.out.println("Target word: " + model.getTargetWord().toUpperCase());
                // Show solution path if enabled
                if (isShowingPath) {
                    List<String> solutionPath = model.getSolutionPath();
                    if (solutionPath != null && !solutionPath.isEmpty()) {
                        System.out.print("\n\033[32mSolution path:\033[0m [");
                        for (String word : solutionPath) {
                            System.out.print(word+" ");
                        }
                    } else {
                        System.out.println("\n\033[31mNo solution path found!\033[0m");
                    }
                    System.out.println("]\n");
                }

                
                // Get player's next word
                System.out.print("Enter current word (or 'quit' to exit): ");
                String input = scanner.nextLine().trim().toLowerCase();
                
                if (input.equals("quit")) {
                    System.out.println("Thanks for playing!");
                    break;
                }
                
                // Validate and process the move
                if (model.isValidMove(input)) {
                    model.makeMove(input);
                    if (model.isGameWon()) {
                        System.out.println("\n\033[33mCongratulations! You have won the game!");
                        System.out.println("Final path: \033[0m" + model.getCurrentPath());
                        System.out.println("\nDo you want to play again? (y/n)");
                        if (!scanner.nextLine().trim().toLowerCase().equals("y")) {
                            break;
                        }
                        main(args);
                        return;
                    }
                } else if (isValidating) {
                    System.out.println("\u001B[31mInvalid move! The word must:");
                    System.out.println("1. Be in the dictionary");
                    System.out.println("2. Differ by exactly one letter from the current word\u001B[0m");
                }
            }
            
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            System.exit(1);
        }
    }
}